package com.test.mooan.labama.utils_boom.http_boom;

import static com.test.mooan.labama.constants_boom.Url_boom.*;
import static com.test.mooan.labama.utils_boom.http_boom.HttpManager_boom.Upload_Info_Url;

import android.text.TextUtils;

import com.test.mooan.labama.activity_fragment_boom.base_boom.BaseActivity_boom;
import com.test.mooan.labama.activity_fragment_boom.base_boom.BaseApplication_boom;
import com.test.mooan.labama.activity_fragment_boom.base_boom.BaseLnActivity_boom;
import com.test.mooan.labama.activity_fragment_boom.base_boom.BaseLoginActivity_boom;
import com.test.mooan.labama.activity_fragment_boom.ServiceFeedbackActivity_boom;
import com.test.mooan.labama.activity_fragment_boom.StartActivity_boom;
import com.test.mooan.labama.activity_fragment_boom.ln_boom.RepaymentActivity;
import com.test.mooan.labama.activity_fragment_boom.ln_boom.StatusPendingActivity;
import com.test.mooan.labama.activity_fragment_boom.ln_boom.StatusRejectedActivity;
import com.test.mooan.labama.activity_fragment_boom.ln_boom.StatusReviewActivity;
import com.test.mooan.labama.activity_fragment_boom.login_boom.MobileVerificationActivity;
import com.test.mooan.labama.activity_fragment_boom.login_boom.MobileVerificationActivity2;
import com.test.mooan.labama.activity_fragment_boom.process_boom.BdBkActActivity_Fr_boom;
import com.test.mooan.labama.activity_fragment_boom.ln_boom.LnApplyActivity_boom;
import com.test.mooan.labama.activity_fragment_boom.ln_boom.LnMainActivity_boom;
import com.test.mooan.labama.bean_boom.config_info_boom.AppShowInfo_boom;
import com.test.mooan.labama.bean_boom.config_info_boom.CountryInfo_boom;
import com.test.mooan.labama.bean_boom.config_info_boom.UrlInfo_boom;
import com.test.mooan.labama.bean_boom.product.ProductListInfo;
import com.test.mooan.labama.bean_boom.star_dialog.StarWindowConfig_boom;
import com.test.mooan.labama.bean_boom.user_info_boom.LnLimitInfo_boom;
import com.test.mooan.labama.bean_boom.user_info_boom.LoginInfo_boom;
import com.test.mooan.labama.bean_boom.user_info_boom.RpH5Info_gn_cl_boom;
import com.test.mooan.labama.bean_boom.user_info_boom.RpH5Info_ug_ke_boom;
import com.test.mooan.labama.bean_boom.user_info_boom.RpInfo_boom;
import com.test.mooan.labama.constants_boom.Url_boom;
import com.test.mooan.labama.dialog_boom.LoadingDialog_boom;
import com.test.mooan.labama.utils_boom.Stringutil_boom;
import com.test.mooan.labama.utils_boom.Threadutils_boom;
import com.test.mooan.labama.activity_fragment_boom.process_boom.BdBkActActivity_Cote_boom;
import com.test.mooan.labama.activity_fragment_boom.process_boom.FaceUploadActivity_boom;
import com.test.mooan.labama.activity_fragment_boom.process_boom.InfoUpldActivity_boom;
import com.test.mooan.labama.activity_fragment_boom.process_boom.OcrUpldActivity_boom;
import com.test.mooan.labama.activity_fragment_boom.base_boom.BaseInfoActivity_boom;
import com.test.mooan.labama.bean_boom.user_info_boom.ActList_boom;
import com.test.mooan.labama.bean_boom.user_info_boom.LnApplyDetailInfo_boom;
import com.test.mooan.labama.bean_boom.process_boom.ProcessInfo_boom;
import com.test.mooan.labama.utils_boom.analytics_boom.event_boom.IAnalyticsEvent_boom;
import com.test.mooan.labama.utils_boom.analytics_boom.util_boom.AnalyticsUtil_boom;

import org.json.JSONArray;
import org.json.JSONObject;


public class HttpPresenter_boom {

    private static class Inner {
        private static final HttpPresenter_boom instance = new HttpPresenter_boom();
    }

    private HttpPresenter_boom() {}

    public static HttpPresenter_boom getInstance() {
        return Inner.instance;
    }

    private BaseActivity_boom activity;

    public LoadingDialog_boom progressBarDialog;

    public String dropType = "GET";

    public void setActivity(BaseActivity_boom activity) {
        if (progressBarDialog != null && progressBarDialog.isShowing()) {
            progressBarDialog.dismiss();
        }
        if (activity != null) progressBarDialog = new LoadingDialog_boom(activity);
        this.activity = activity;
    }

    public void progressBarDialogShow() {
        if (progressBarDialog == null) return;
        progressBarDialog.show();
    }

    public void progressBarDialogDismiss() {
        if (progressBarDialog == null) return;
        progressBarDialog.dismiss();
    }

    public void showToastOnUiThread(String text) {
        if (activity != null) {
            activity.showToast_boom(text);
        }
    }

    //通知Activity处理响应
    public void notifyActivityHandleResponse(String word, JSONObject responseJson,JSONObject requestJson) {

        if (activity == null) return;

        try {
            String gcode = responseJson.optString("code");
            String gmsg = Stringutil_boom.getSafeString(responseJson.optString("msg"));
            Object gobj = responseJson.opt("obj");

            //失败响应
            if (Stringutil_boom.safeParseInt(gcode)!=0) {
                onResponseFail(word,gmsg);
                return;
            }

            //成功响应
            onResponseSuccess(word,gmsg,gobj,requestJson);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //成功响应
    private void onResponseSuccess(String word,String gmsg,Object gobj,JSONObject requestJson){

        if (gobj instanceof JSONObject objJsonObject) {

            //流程数据上传
            if (!TextUtils.isEmpty(Upload_Info_Url)&&word.endsWith(Upload_Info_Url)){
                if (activity instanceof BaseInfoActivity_boom) ((BaseInfoActivity_boom) activity).onInfoUploadResponse_boom();
                return;
            }

            if (word.endsWith(PATH_REQUEST_URLS)){//获取url
                UrlInfo_boom.parse(objJsonObject);
                AnalyticsUtil_boom.INSTANCE.initDevice(BaseApplication_boom.getInstant_boom());
                if (activity instanceof BaseLoginActivity_boom) ((BaseLoginActivity_boom)activity).onUrlsResponse();
                return;
            }

            //获取配置信息
            if (Stringutil_boom.getSafeString(word).endsWith(PATH_APP_SHOW_INFO)){
                AppShowInfo_boom.parse(objJsonObject);
                if (activity instanceof StartActivity_boom) ((StartActivity_boom) activity).onAppShowInfoResponse();
                else if (activity instanceof LnMainActivity_boom) ((LnMainActivity_boom)activity).onAppShowInfoResponse();
                return;
            }

            //获取验证码
            if (Stringutil_boom.getSafeString(word).endsWith(PATH_SMS_CODE)){
                BaseApplication_boom.getInstant_boom().setMsg(gmsg);
                if (activity instanceof BaseLoginActivity_boom) ((BaseLoginActivity_boom) activity).onRequestOtpCodeResponse_boom(objJsonObject);
                return;
            }

            //用户登录
            if (Stringutil_boom.getSafeString(word).endsWith(PATH_USER_LOGIN)){
                LoginInfo_boom.parse(objJsonObject);
                if (activity instanceof BaseLoginActivity_boom) ((BaseLoginActivity_boom) activity).onUserLoginResponse_boom();
                return;
            }

            //用户登录-无验证码
            if (Stringutil_boom.getSafeString(word).endsWith(PATH_LOGIN_NO_CODE)){
                LoginInfo_boom.parse(objJsonObject);
                if (activity instanceof BaseLoginActivity_boom) ((BaseLoginActivity_boom) activity).onUserLoginResponse_boom();
                return;
            }

            //提交反馈
            if (Stringutil_boom.getSafeString(word).endsWith(PATH_FEED_BACK)){
                if (activity instanceof ServiceFeedbackActivity_boom) ((ServiceFeedbackActivity_boom) activity).onSubmitServiceFeedback();
                return;
            }

            //上传图片
            if (Stringutil_boom.getSafeString(word).endsWith(PATH_UPLOAD_IMAGE_FILE)){
                String url = objJsonObject.optString("url");
                if (activity instanceof FaceUploadActivity_boom) ((FaceUploadActivity_boom)activity).onPhotoUploadResponse(url);
                else if (activity instanceof OcrUpldActivity_boom) ((OcrUpldActivity_boom)activity).onPhotoUploadResponse(url);
                return;
            }

            //用户流程状态
            if (Stringutil_boom.getSafeString(word).endsWith(UrlInfo_boom.gInstance().gJaee())){
                ProcessInfo_boom.parse(objJsonObject.toString());
                activity.onUserProcessResponse();
                return;
            }

            //额度信息
            if (word.endsWith(UrlInfo_boom.gInstance().gH8e7())){
                LnLimitInfo_boom.parse(objJsonObject);
                if (activity instanceof LnMainActivity_boom) ((LnMainActivity_boom) activity).onLoanLimitInfoResponse();
                return;
            }

            //申请页信息
            if (word.endsWith(UrlInfo_boom.gInstance().gLrt0())){
                LnApplyDetailInfo_boom.parse(objJsonObject);
                activity.onLoanApplyDetailResponse();
                return;
            }

            //发起借款申请响应
            if (word.endsWith(UrlInfo_boom.gInstance().gA1ga())){
                AnalyticsUtil_boom.INSTANCE.addEvent(IAnalyticsEvent_boom.AT_LOAN_APPLY);
                if (activity instanceof LnApplyActivity_boom) ((LnApplyActivity_boom) activity).onLoanApplyResponse();
                return;
            }

            //还款信息展示
            if (word.endsWith(UrlInfo_boom.gInstance().gP3hr())){
                RpInfo_boom.parse(objJsonObject);
                activity.onRepayDetailResponse();
                return;
            }

            //获取还款Url
            if (word.endsWith(UrlInfo_boom.gInstance().gAe2e())){
                RpH5Info_gn_cl_boom.parse(objJsonObject);
                if (activity instanceof RepaymentActivity) ((RepaymentActivity) activity).onOrderRepayH5Response(RpH5Info_gn_cl_boom.gInstance().gUrl());
                return;
            }

            //获取还款Url（乌干达、肯尼亚）
            if (word.endsWith(UrlInfo_boom.gInstance().gQjy9())){
                RpH5Info_ug_ke_boom.parse(objJsonObject);
                if (activity instanceof RepaymentActivity) ((RepaymentActivity) activity).onOrderRepayH5Response(RpH5Info_ug_ke_boom.getInstance().gUrl());
                return;
            }

            //获取TGBOT Url
            if (word.endsWith(Url_boom.PATH_GET_TGBOT_URL)){
                if (activity instanceof MobileVerificationActivity) ((MobileVerificationActivity)activity).onTgBotUrlResponse(objJsonObject.optString("url"));
                else if (activity instanceof MobileVerificationActivity2) ((MobileVerificationActivity2)activity).onTgBotUrlResponse(objJsonObject.optString("url"));
                return;
            }

            //删除用户数据
            if (word.endsWith(PATH_DELETE_USER_DATA_2)){
                if (activity!=null) {
                    if ("DROP".equalsIgnoreCase(dropType)) activity.onUserDataDelete();
                    else{
                        String showDropButton = objJsonObject.optString("showDropButton");
                        if (activity instanceof LnMainActivity_boom)
                            ((LnMainActivity_boom) activity).onDropConfigResponse("YES".equalsIgnoreCase(showDropButton) || "true".equalsIgnoreCase(showDropButton));
                        else if (activity instanceof StatusRejectedActivity)
                            ((StatusRejectedActivity)activity).onDropConfigResponse("YES".equalsIgnoreCase(showDropButton) || "true".equalsIgnoreCase(showDropButton));
                    }
                }
                return;
            }

            //评分弹窗配置
            if (word.endsWith(PATH_FETCH_STAR_WINDOW_CONFIG)){
                StarWindowConfig_boom.parseData(objJsonObject);
                if (activity instanceof BaseLnActivity_boom) ((BaseLnActivity_boom)activity).onStarWindowConfigResponse();
                return;
            }

            //后置校验-发送验证码
            if (word.endsWith(Url_boom.PATH_GET_CODE)){
                if (activity instanceof MobileVerificationActivity)
                    ((MobileVerificationActivity) activity).onGetCodeSuccessful();
                else if (activity instanceof MobileVerificationActivity2)
                    ((MobileVerificationActivity2) activity).onGetCodeSuccessful();
                return;
            }

            //后置校验-校验
            if (word.endsWith(Url_boom.PATH_CHECK_MOBILE)){
                if (activity instanceof MobileVerificationActivity)
                    ((MobileVerificationActivity) activity).onCheckSuccessful();
                else if (activity instanceof MobileVerificationActivity2)
                    ((MobileVerificationActivity2) activity).onCheckSuccessful();
                return;
            }

            //删除钱包（科特、喀麦隆、塞内加尔）
            if (Stringutil_boom.getSafeString(word).endsWith(UrlInfo_boom.gInstance().gaUrf())){
                if (activity instanceof BdBkActActivity_Cote_boom) ((BdBkActActivity_Cote_boom) activity).onDeleteBankAccountResponse();
                else if (activity instanceof BdBkActActivity_Fr_boom) ((BdBkActActivity_Fr_boom) activity).onDeleteBankAccountResponse();
                else if (activity instanceof StatusReviewActivity) ((StatusReviewActivity)activity).onDeleteBankAccountResponse();
                else if (activity instanceof StatusPendingActivity) ((StatusPendingActivity)activity).onDeleteBankAccountResponse();
                return;
            }

            //新增钱包（科特、喀麦隆、塞内加尔）
            if (Stringutil_boom.getSafeString(word).endsWith(UrlInfo_boom.gInstance().gHner())){
                if (activity instanceof BdBkActActivity_Cote_boom) ((BdBkActActivity_Cote_boom) activity).onBindBankAccountResponse();
                else if (activity instanceof BdBkActActivity_Fr_boom) ((BdBkActActivity_Fr_boom) activity).onBindBankAccountResponse();
                else if (activity instanceof StatusReviewActivity) ((StatusReviewActivity)activity).onBindBankAccountResponse();
                else if (activity instanceof StatusPendingActivity) ((StatusPendingActivity)activity).onBindBankAccountResponse();
                return;
            }

            //保存数据
//            if (Stringutil_boom.getSafeString(word).endsWith(PATH_SAVE_DATA)){}

            //读取数据
//            if (Stringutil_boom.getSafeString(word).endsWith(PATH_GET_DATA)){}

            //复制产品流程信息（贷超）
            if (word.endsWith(Url_boom.PATH_COPY_PROCESS_INFO)){
                if (activity instanceof LnMainActivity_boom) ((LnMainActivity_boom)activity).onCopyProcessInfoSuccessful();
                return;
            }


        } else if (gobj instanceof JSONArray jsonArray) {

            //获取支持的绑卡银行列表（尼日）
            if (word.endsWith(UrlInfo_boom.gInstance().gN01u())){
                if (activity instanceof InfoUpldActivity_boom) ((InfoUpldActivity_boom) activity).onFetchBankCodeResponse(jsonArray);
                return;
            }

            //获取钱包列表（科特、喀麦隆、塞内加尔）
            if (word.endsWith(UrlInfo_boom.gInstance().gCas5())){
                ActList_boom.parse(jsonArray);
                activity.onFetchBankAccountListResponse();
                return;
            }

            if (word.endsWith(PATH_QUERY_PRODUCTS)){
                ProductListInfo.stInstance(jsonArray);
                if (activity instanceof LnMainActivity_boom) ((LnMainActivity_boom)activity).onQueryProductsSuccessful();
                return;
            }

        } else {
            //其它类型
            gmsg = Stringutil_boom.getSafeString(gmsg);
            if (TextUtils.isEmpty(gmsg)) {
                gmsg = "Response something error";
            }
            activity.showToast_boom(gmsg);
        }
    }

    //失败响应
    private void onResponseFail(String word,String gmsg){
        activity.showToast_boom(gmsg);
//        switch (word){
//            case PATH_UPLOAD_IMAGE_FILE://图片上传失败
//
//                break;
//        }


        //后置校验-校验
        if (word.endsWith(Url_boom.PATH_CHECK_MOBILE)){
            if (activity instanceof MobileVerificationActivity)
                ((MobileVerificationActivity) activity).onCheckFail();
            else if (activity instanceof MobileVerificationActivity2)
                ((MobileVerificationActivity2) activity).onCheckFail();
        }

        //复制产品流程信息（贷超）
        if (word.endsWith(Url_boom.PATH_COPY_PROCESS_INFO)){
            if (activity instanceof LnMainActivity_boom) ((LnMainActivity_boom)activity).onCopyProcessInfoFailed();
            return;
        }

        //用户登录-无验证码
        if (Stringutil_boom.getSafeString(word).endsWith(PATH_LOGIN_NO_CODE) && !TextUtils.isEmpty(gmsg)){
            if (activity!=null) activity.showMsgDialog(gmsg);
            return;
        }

    }

    public void onTimezoneResponse(JSONObject jsonObject){

        String msg = jsonObject.optString("msg");
        if (msg.equalsIgnoreCase("success")){
            String[] data = jsonObject.optString("data").split(",");
            String front = "";
            String mobileprefix = "";
            if (data.length>0) front = data[0];
            if (data.length>1) mobileprefix = data[1];
            CountryInfo_boom.gInstance().sFront(front);
            CountryInfo_boom.gInstance().sAreaCode(mobileprefix);
            if (activity instanceof StartActivity_boom) ((StartActivity_boom)activity).onTimezoneResponse();
        }else Threadutils_boom.getInstance().runOnUiThread(() -> activity.showToast_boom(msg));
    }

}
